package com.android.supermarket.helper

import android.content.Context
import java.io.BufferedReader
import java.io.IOException
import java.io.OutputStream
import java.net.InetSocketAddress
import java.net.Socket

/**
 * Created by pengtaoH on 2018/7/19.
 */
class WIFISocketHelper(context: Context) {

    val MESSAGE_CONNECTED = true
    val MESSAGE_CONNECTED_ERROR = false
    val MESSAGE_WRITE_SUCCESS = true
    val MESSAGE_WRITE_ERROR = false
    private var mMyWifiSocket: Socket? = null
    private var BufReader: BufferedReader? = null
    private var PriOut: OutputStream? = null
    private var iState = false

    var mstrIp = "192.168.2.150"
    var mPort = 9100

    internal var TimeOut = 1300
    fun getIstate(): Boolean {
        return iState
    }

    fun threadconnect() {
        ConnectThread()
    }

    fun threadconnectwrite(str: ByteArray) {
        WriteThread(str)
    }

    fun connect(): Boolean {
        close()
        try {
            mMyWifiSocket = Socket()
            mMyWifiSocket!!.connect(InetSocketAddress(mstrIp, mPort), TimeOut)
            PriOut = mMyWifiSocket!!.getOutputStream()
            return true
        } catch (e: IOException) {
            e.printStackTrace()
            SetState(MESSAGE_CONNECTED_ERROR)
            return false
        }

    }


    fun write(out: ByteArray): Boolean {
        return if (PriOut != null) {
            try {
                PriOut!!.write(out)
                PriOut!!.flush()
                true
            } catch (e: IOException) {
                e.printStackTrace()
                false
            }

        } else {
            false
        }
    }

    fun close() {
        if (mMyWifiSocket != null) {
            try {
                mMyWifiSocket!!.close()
                mMyWifiSocket = null
            } catch (e1: IOException) {
                e1.printStackTrace()
            }

        }
        if (BufReader != null) {
            try {
                BufReader!!.close()
                BufReader = null
            } catch (e2: IOException) {
                e2.printStackTrace()
            }

        }
        if (PriOut != null) {
            try {
                PriOut!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

            PriOut = null
        }
    }

    fun ConnectAndWrite(out: ByteArray): Boolean {
        if (connect()) {
            write(out)
            close()
            SetState(MESSAGE_WRITE_SUCCESS)
            return true
        } else {
            SetState(MESSAGE_CONNECTED_ERROR)
            return false
        }
    }


    fun SetState(state: Boolean?) {
        iState = state!!
    }

    private inner class ConnectThread : Thread() {
        init {
            start()
        }

        override fun run() {
            if (connect()) {
                SetState(MESSAGE_CONNECTED)
            }
            close()
        }
    }

    private inner class WriteThread(internal var out: ByteArray) : Thread() {
        init {
            start()
        }

        override fun run() {
            if (ConnectAndWrite(out)) {
                SetState(MESSAGE_WRITE_SUCCESS)
            }
        }
    }
}
